<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            margin: 0 auto;
            padding: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        .layout {
            height: 100%;
            width: 100%;
            display: -webkit-flex;
            display: flex;
            flex-direction: row;
            position: relative;
        }


        .layout-main {
            height: 100%;
            width: 100%;
            background-color: white;
        }

        #myCanvas {
            margin: 0 auto;
            background: lightslategray;
        }

        .debug_tools_container {
            width: 400px;
            height: 50px;
            line-height: 50px;
            background-color: lightslategray;
            position: relative;
            bottom: 100px;
            margin: 0 auto;
            position: absolute;
            margin-left: 50%;
            transform: translateX(-200px);
            background-color: white;
            border: 1px orange solid;
            text-align: center;
            display: flex;
            align-content: center;
            align-items: center;
        }

        .debug_tools_container a {
            flex: 1;
            background-color: lightseagreen;
            color: white;
            height: 30px;
            line-height: 30px;
            margin: 0px 5px;
        }
    </style>
    <script src="https://cdn.bootcss.com/fabric.js/3.2.0/fabric.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

    <div class="layout">
        <div class="layout-main">
            <canvas id="myCanvas"></canvas>
        </div>
    </div>

    <div class="debug_tools_container">
        <a href="###" id="getJson">保存</a>
        <a href="###" id="preShow">预览</a>
        <a href="###" id="loadJson">返渲染</a>
    </div>

    <script>
        function printf() {
            console.log(arguments);
        }

        var windowOption = {
            currentCanvas: {
                width: 0,
                height: 0
            }
        };

        window.onload = function () {


        }

        $(function () {

            windowOption.currentCanvas.width = $(".layout-main").width();
            windowOption.currentCanvas.height = $(".layout-main").height();

            printf(windowOption, "==========windowOption=========");

            var c = document.getElementById("myCanvas");

            c.width = 800;
            c.height = 500;

            // c.style.marginTop = windowOption.currentCanvas.height / 2 - c.height / 2 + "px";
            // c.style.marginLeft = windowOption.currentCanvas.width / 2 - c.width / 2 + "px";

            var canvas = new fabric.Canvas('myCanvas');
            canvas.width = c.width;
            canvas.height = c.height;


            $("#getJson").click(function () {
                // 导出当前画布信息
                const json = canvas.toJSON(); // 导出的Json如下图

                console.log(currState, "=======currState==========");

                var currState = {
                    canvas_json: json,
                    canvas_width: c.width,
                    canvas_height: c.height
                }

                localStorage.setItem("currState", JSON.stringify(currState));
            });


            $("#loadJson").click(function () {
                var currState = JSON.parse(localStorage.getItem("currState"));
                // 加载画布信息
                canvas.loadFromJSON(JSON.stringify(currState.canvas_json), () => {

                    canvas.setZoom(1.0);

                    var zoom_rate = c.width / c.height * currState.width /

                        canvas.renderAll();
                });

            });


            $("#preShow").click(function () {


            });






        });
    </script>

</body>

</html>